﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace mytest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            int x = (System.Windows.Forms.SystemInformation.WorkingArea.Width - this.Size.Width) / 2;
            int y = (System.Windows.Forms.SystemInformation.WorkingArea.Height - this.Size.Height) / 2;
            this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
            this.Location = (Point)new Size(x, y);         //窗体的起始位置为(x,y)

            this.listView1.SmallImageList = this.imageList1;
            this.getUserInfo();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void getUserInfo()
        {
            var client = new RestClient("http://localhost:1010");

            var request = new RestRequest("api/dotnet/getUser", Method.POST);
            int uid = 1;
            request.AddParameter("uid", uid);

            // execute the request
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string

            var UserjsonObj = JsonConvert.DeserializeObject<dynamic>(content);
            if(UserjsonObj.error == false)
            {
                this.name.Text = UserjsonObj.data.name;
                this.sex.Text = UserjsonObj.data.sex == 1 ? "男" : "女";
                this.level.Text = UserjsonObj.data.level;
                this.avatar.ImageLocation = UserjsonObj.data.avatar;
                JArray friends = UserjsonObj.data.friends;
                this.renderListView(friends);
                //Console.WriteLine(UserjsonObj.data.friends[0].name);
            }
            else
            {
                MessageBox.Show("获取失败");
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count == 0) return;
            int n = listView1.SelectedIndices[0];
            Console.WriteLine(listView1.Items[n].SubItems[1].Text);
            chatname.Text = listView1.Items[n].SubItems[2].Text;

        }

        private void renderListView(JArray friends)
        {
            foreach (JObject item in friends)
            {
                Console.WriteLine(item["name"]);

                ListViewItem lvi = new ListViewItem();

                lvi.ImageIndex = (int)item["avatar"];

                lvi.Text = (string)item["name"];
                lvi.SubItems.Add((string)item["uid"]);
                lvi.SubItems.Add((string)item["name"]);

                this.listView1.Items.Add(lvi);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String chats = chatbox.Text;
            chatbox.Text = "";
            MessageBox.Show(chats);
        }
    }
}
